home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_111 / assigndev / misc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  332 lines

  1. /* misc.c 
  2.  *      Misc. subroutines that like to live together...
  3.  *
  4.  * Phillip Lindsay (c) 1987 Commodore-Amiga - This code may be freely used 
  5.  * as long as the copyright notice is left intact.
  6.  */
  7.  
  8.                                                             /* V17.08.87 */
  9. /* Some small changes and a bug fix were made by Olaf Seibert, KosmoSoft */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/nodes.h>
  13. /* #include <exec/lists.h> */
  14. #include <exec/memory.h>
  15. #include <exec/ports.h>
  16. #include <exec/libraries.h>
  17. /* #include <exec/devices.h> */
  18. /* #include <exec/io.h> */
  19. /* #include <devices/console.h> */
  20. /* #include <intuition/intuition.h> */
  21. #include <libraries/dos.h>
  22. #include <libraries/dosextens.h>
  23. #include <libraries/filehandler.h>
  24.  
  25. #ifdef AZTEC_C
  26. #include <functions.h>
  27. #endif
  28.  
  29. #define MKBPTR(x)       ((BPTR)((long)x >> 2))
  30.  
  31. /* btoc() takes a pointer to a BSTR and converts it to a
  32.  * C string.
  33.  */
  34. char *btoc(bstring)
  35. CPTR bstring;
  36. {
  37.  register UBYTE len,count,*cstring=NULL;
  38.  
  39.  if (bstring) {
  40.     cstring = (UBYTE *) bstring;
  41.     len = cstring[0];              
  42.     for(count=0;count < len;count++)
  43.         cstring[count] = cstring[count+1];
  44.         
  45.     cstring[count] = '\0';
  46.  }
  47.  
  48.  
  49.  return((char *)cstring);
  50. }
  51.  
  52. /* ctob() takes a pointer to a C string and converts 
  53.  * it to a BSTR.
  54.  */
  55. BSTR ctob(cstring)
  56. UBYTE *cstring;
  57. {
  58.  extern   UBYTE strlen();
  59.  register UBYTE len,count; 
  60.  
  61.  if (cstring) {
  62.     len = strlen(cstring);              
  63.     for(count=len;count > (UBYTE)0;count--)
  64.         cstring[count] = cstring[count-1];
  65.     cstring[count] = len;
  66.  }
  67.  
  68.  return((BSTR)MKBPTR(cstring));
  69. }
  70.  
  71. /*
  72.  * memcpy() - fast "C" byte memory copy.
  73.  */
  74. VOID memcpy(source,dest,size)
  75. UBYTE *source,*dest;
  76. ULONG size;
  77. {
  78.     register UBYTE  *msource=source,
  79.                     *mdest=dest;
  80.     register ULONG  msize=size;
  81.  
  82.     if (msize) {
  83.         do { *mdest++ = *msource++; } while(--msize);
  84.     }
  85.         
  86. }
  87.  
  88. /*
  89.  * memfill() - fill memory with supplied byte value for "size" bytes
  90.  */
  91. VOID memfill(source,size,value)
  92. UBYTE *source;
  93. ULONG size;
  94. UBYTE value;    
  95. {
  96.     register UBYTE  *msource=source,
  97.                     mvalue=value,
  98.                     *mend;
  99.  
  100.  for(mend = &source[size]; msource < mend ;msource++)
  101.     *msource = mvalue;
  102. }
  103.  
  104.  
  105. /*
  106.  * strupper() - return given string in upper case;
  107.  *
  108.  */ 
  109.  
  110. char *strupper(astring)
  111. char *astring; 
  112. {
  113.     register char *astr = astring;
  114.     
  115.     if (astr) {
  116.         do {
  117.             *astr = toupper(*astr);
  118.         } while(*astr++);
  119.     }
  120.  
  121.     return(astring);    
  122. }
  123.  
  124. /*
  125.  * trunc() - truncate string at specified character
  126.  *
  127.  */
  128.  VOID trunc(astring,acharacter)
  129.  char *astring,acharacter;
  130.  {
  131.     register char *astr=astring,achar=acharacter;
  132.     
  133.     do {
  134.         if ( *astr == achar ) {
  135.             *astr = '\0';
  136.             break;
  137.         }
  138.     } while(*astr++);       
  139.  }
  140.  
  141.  
  142. /*
  143.  * allocstr() - copies a string into a newly allocated memory space.
  144.  *
  145.  */
  146. char *allocstr(astring,length) 
  147. char    *astring;
  148. ULONG   length;
  149. {
  150.     register    char    *astr=astring,
  151.                         *newstr=NULL;
  152.     register    ULONG   slength=length;
  153.     
  154.     if ( (astr) && (slength) ) {
  155.         if ( ( newstr = ( char * ) AllocMem( slength, MEMF_PUBLIC) ) )
  156.             memcpy(astr,newstr,slength);
  157.     }
  158.     
  159.     
  160.     return(newstr);
  161. }
  162.  
  163. /* OIS V09.08.87 */
  164. #define KEYWORD ( ((ULONG)'C' << 24) | ((ULONG)'O' << 16) | ('P' << 8) | ('Y') )
  165.  
  166. /*
  167.  * check to see if device node was created by dupdev().
  168.  */
  169.  BOOL ourdevnode(dnode)
  170.  struct DeviceNode *dnode;
  171.  {
  172.     register    UBYTE   length;
  173.     char                *bname;
  174.     ULONG               key;
  175.  
  176.     
  177.     bname   = (char *)  BADDR(dnode->dn_Name);
  178.     length  = (UBYTE)   bname[0] + 1;
  179.     key     =           KEYWORD;
  180.         
  181.     /* OIS V09.08.87 */
  182.     return ( (BOOL) ( !strncmp(&key,&bname[length],(int)sizeof(ULONG)) ?
  183.         TRUE : FALSE ) );
  184.  }
  185.     
  186.  
  187. /* 
  188.  * free memory we allocated for duplicate device node. 
  189.  */  
  190. VOID freedev(dnode)
  191. struct DeviceNode *dnode;
  192. {
  193.     register    char    *devname;
  194.     register    UBYTE   length;
  195.     
  196.     if (dnode->dn_Name) {
  197.         devname =   btoc( BADDR(dnode->dn_Name) );
  198.         length  =   (UBYTE) strlen(devname);
  199.         length +=   5;                  
  200.         FreeMem(devname,(ULONG) length);
  201.     }
  202.         
  203.     FreeMem( dnode, (ULONG) sizeof(*dnode) );   
  204. }
  205.  
  206.  
  207. /*
  208.  *  Add a dos node to the system device list
  209.  *
  210.  */
  211. VOID adddosnode(newdnode)
  212. struct DeviceNode *newdnode;
  213. {
  214.     extern   struct DosLibrary *DOSBase;
  215.     register struct RootNode   *rnode;
  216.     register struct DosInfo    *dinfo;
  217.     register struct DeviceNode *dnode;  
  218.     register BPTR              *dnodelink;
  219.      
  220.     rnode   = (struct RootNode *)  DOSBase->dl_Root;        /* find root node */
  221.     dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);   /* now dos info   */
  222.     dnodelink = &dinfo->di_DevInfo;     /* Address of BPTR to next DeviceNode */
  223.     
  224.     Forbid();
  225.  
  226.     /* OIS V16.08.87 */
  227.     /* Traverse the list until the first real DeviceNode shows up */
  228.     while (dnode = (struct DeviceNode *) BADDR(*dnodelink)) {
  229.         if (dnode->dn_Type == DLT_DEVICE) break;
  230.         dnodelink = &dnode->dn_Next;
  231.     }
  232.                 
  233.     /* Insert it just before the first device, so we can override */
  234.     /* existing device names, notably ASSIGNDEV RAM: VD0: */
  235.     /* but don't collide with Volume and Directory names too much. */
  236.     /* In the unlikely case there is no DeviceNode, append it at the end */
  237.  
  238.     newdnode->dn_Next = MKBPTR(dnode);
  239.     *dnodelink        = MKBPTR(newdnode);
  240.  
  241.     Permit();        
  242. }
  243.  
  244.  
  245. /*
  246.  *  find a dos node in the system device list
  247.  *
  248.  */
  249. struct DeviceNode *finddosnode(lookfor)
  250. char    *lookfor;
  251. {
  252.     extern   struct DosLibrary *DOSBase;
  253.              struct RootNode   *rnode;
  254.              struct DosInfo    *dinfo;
  255.     register struct DeviceNode *dnode;
  256.     register char              *bname;  
  257.     char                        name1[81];
  258.     char                        name2[81];
  259.     BOOL                        found = FALSE;
  260.      
  261.     rnode   = (struct RootNode *)  DOSBase->dl_Root;        /* find root node */
  262.     dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);   /* now dos info   */
  263.     
  264.     strcpy(name1,lookfor);
  265.     strupper(name1);
  266.  
  267.     Forbid();
  268.  
  269.     for(dnode = (struct DeviceNode *) BADDR(dinfo->di_DevInfo); ( dnode ) ;
  270.       dnode = (struct DeviceNode *) BADDR(dnode->dn_Next))
  271.     {
  272.         bname = (char *) BADDR(dnode->dn_Name);
  273.         memcpy(bname, name2, (ULONG)( bname[0] + 1 ) );
  274.         
  275.         if ( !( strcmp( name1, strupper(btoc(name2)) ) ) ) {
  276.             found   =   TRUE;
  277.             break; 
  278.         } 
  279.  
  280.     }
  281.  
  282.     Permit(); 
  283.     
  284.     return dnode;       /* NULL if not found */
  285. }
  286.  
  287.  
  288. /* 
  289.  *  Duplicate the given device node and add it the system device list.
  290.  */
  291. VOID dupdev(dnode,newname)
  292. struct DeviceNode   *dnode;
  293. char                *newname;
  294. {
  295.     register struct DeviceNode  *newdnode;
  296.     register BSTR               newbname;
  297.     register UBYTE              length;
  298.     char                        tmpname[81];
  299.     ULONG                       tmplong;
  300.         
  301.     newdnode = (struct DeviceNode *)
  302.         AllocMem( (ULONG) sizeof(*newdnode), MEMF_PUBLIC | MEMF_CLEAR );
  303.     
  304.     memcpy(dnode,newdnode,(ULONG)sizeof(*newdnode));
  305.     
  306.     newdnode->dn_Name   = NULL; 
  307.   
  308.     strcpy(tmpname,newname);
  309.     
  310.     length              = (UBYTE) strlen(tmpname);
  311.      
  312.     tmplong             = KEYWORD;
  313.     
  314.     memcpy(&tmplong,&tmpname[length+1], (ULONG) sizeof(ULONG) );
  315.   
  316.     length             += 5;    /* include NULL and LONG WORD */
  317.     
  318.     newbname            = ctob( allocstr(tmpname, (ULONG) length) );
  319.     
  320.     newdnode->dn_Next   = NULL;
  321.    
  322.     if (!newbname)
  323.         freedev(newdnode);
  324.     else {    
  325.         newdnode->dn_Name = newbname;
  326.         adddosnode(newdnode);
  327.     }
  328. }
  329.  
  330.  
  331. /* end of misc.c */
  332.